package com.sanrenzu.everybodytv.ui.mine.recommend;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.ui.recommend_umeng.RecommendLandActivity;
import com.sanrenzu.everybodytv.widget.GlideRoundTransform;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by My on 2016/9/5.
 */
public class RecommendMineFragment extends Fragment {
    //房间管理
    @BindView(R.id.linelayout_mine_room)
    LinearLayout linelayoutMineRoom;
    //我的关注
    @BindView(R.id.linelayout_mine_guanzhu)
    LinearLayout linelayoutMineGuanzhu;
    //观看历史
    @BindView(R.id.linelayout_mine_lishi)
    LinearLayout linelayoutMineLishi;
    //开播提醒
    @BindView(R.id.linelayout_mine_kaibo)
    LinearLayout linelayoutMineKaibo;
    //种子任务
    @BindView(R.id.linelayout_mine_zhongzi)
    LinearLayout linelayoutMineZhongzi;
    //游戏中心
    @BindView(R.id.linelayout_mine_youxi)
    LinearLayout linelayoutMineYouxi;
    //充值
    @BindView(R.id.linelayout_mine_chongzhi)
    LinearLayout linelayoutMineChongzhi;
    //充值
    @BindView(R.id.button_mine_chongzhi)
    Button buttonMineChongzhi;
    //姓名
    @BindView(R.id.textView_mine_name)
    TextView textViewMineName;
    private ImageView imageView_photo;

    private ImageView imageView_message;
    private Context mContext;
    private RequestManager manager;
    private SharedPreferences preferences;
    //声明Editor
    private SharedPreferences.Editor editor;
    private static final String LOHGIN = "AA";
    private String url ;
    private String name ;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        preferences = mContext.getSharedPreferences(LOHGIN, mContext.MODE_PRIVATE);
        editor = preferences.edit();
    }

    //为了保证代码的复用性和单例
    public static RecommendMineFragment newInstance() {
        RecommendMineFragment fragment = new RecommendMineFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        imageView_photo = (ImageView) view.findViewById(R.id.imageView_photo);
        imageView_message = (ImageView) view.findViewById(R.id.imageView_message);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        url = preferences.getString("url", null);
        name = preferences.getString("name", null);
        //加载头像
        if (url != null) {
            Glide.with(mContext).load(url)
                    .transform(new GlideRoundTransform(mContext, 90))
                    .into(imageView_photo);
            linelayoutMineChongzhi.setVisibility(View.VISIBLE);
            //支付还没写

        }else{
            imageView_photo.setImageResource(R.mipmap.img_profile_touxiang_default_logged);
        }
        if(name!=null){
            textViewMineName.setText(name);
        }else{
            textViewMineName.setText("");
        }

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initSetting();
    }

    private void initSetting() {
        if(url==null) {
            imageView_photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(url==null) {
                        Intent intent = new Intent(mContext, RecommendLandActivity.class);
                        mContext.startActivity(intent);
                    }else{
                        Intent intent = new Intent(mContext,LogOutActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("URL",url);
                        bundle.putString("NAME",name);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                }
            });
        }

        buttonMineChongzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,PayActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
